package com.grsu.TransportSystem.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.grsu.TransportSystem.constants.Roles;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(HttpServletRequest request) {
		String result = "redirect:/login";
		List<GrantedAuthority > list = (List<GrantedAuthority>) SecurityContextHolder.getContext().getAuthentication().getAuthorities();
		//request.getSession().setAttribute("userId", arg1);
		for (GrantedAuthority grantedAuthority : list) {
			if(Roles.ADMIN.equals(grantedAuthority.getAuthority()))
			{
				result = "ADMINdesktop";
				break;
			}
			else if(Roles.MANAGER.equals(grantedAuthority.getAuthority()))
			{
				result = "MANAGERdesktop";
			}
			else if(Roles.DRIVER.equals(grantedAuthority.getAuthority()))
			{
				result = "DRIVERdesktop";
			}
		}
		return result;
	}
	
}
